package de.akabahn.ebd.control;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import de.akabahn.ebd.control.auth.AbstractAuthenticator;
import de.akabahn.ebd.control.auth.AccessEnum;
import de.akabahn.ebd.control.auth.RemoteAuthenticator;

/**
 * @author Peter Eimann
 */
public class EBD_ChangePasswordActivity extends EBD_DefaultUncaughtExceptionHandler {

	protected String userName;
	protected AccessEnum role;
	private AbstractAuthenticator authenticator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.changepassword);
		userName = getIntent().getExtras().getString("de.akabahn.ebd.control.userName");
		role = AccessEnum.valueOf(getIntent().getExtras().getString("de.akabahn.ebd.control.roleName"));
		authenticator = RemoteAuthenticator.getInstance();

		Button change = (Button) findViewById(R.id.changepassword_change_button);
		change.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changePassword();
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();

		initializeFields();
		TextView userInfo = (TextView)findViewById(R.id.changepasssord_userInfo);
		userInfo.setText(getResources().getString(R.string.userInfo, userName, role));
	}

	private void changePassword() {
		String oldPassword = ((EditText)findViewById(R.id.changepassword_oldpassword)).getEditableText().toString();
		String newPassword = ((EditText)findViewById(R.id.changepassword_newpassword)).getEditableText().toString();
		String newPassword2 = ((EditText)findViewById(R.id.changepassword_newpassword2)).getEditableText().toString();

		if (TextUtils.isEmpty(newPassword)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_newPasswordEmpty), Toast.LENGTH_LONG).show();
		} else if (!newPassword.equals(newPassword2)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_passwordsNotEqual), Toast.LENGTH_LONG).show();
		} else if (!authenticator.changePassword(userName, oldPassword, newPassword)) {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_changeFailed), Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(EBD_ChangePasswordActivity.this,
					getResources().getString(R.string.changepassword_changeOk), Toast.LENGTH_SHORT).show();
			initializeFields();
		}
	}

	private void initializeFields() {
		((EditText) findViewById(R.id.changepassword_oldpassword)).setText("");
		((EditText) findViewById(R.id.changepassword_newpassword)).setText("");
		((EditText) findViewById(R.id.changepassword_newpassword2)).setText("");
		((EditText) findViewById(R.id.changepassword_oldpassword)).requestFocus();
	}
}